package com.jiaxu.wifimusic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by jiaxu on 2016-3-25.
 */
public class PlayListAdapter extends BaseAdapter{

    private List<MusicInfo> data ;
    private Context context;
    private PlayController playController;
    public PlayListAdapter(Context context,List<MusicInfo> data){
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return data.get(position).getId();
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        LayoutInflater inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.listitem,null);

        MusicInfo musicInfo = data.get(position);
        TextView artist = (TextView)view.findViewById(R.id.music_Artist);
        TextView title = (TextView)view.findViewById(R.id.music_title);
        TextView duration = (TextView)view.findViewById(R.id.music_duration);

        artist.setText(musicInfo.getArtist());
        title.setText(musicInfo.getTitle());
        duration.setText(FormatTime.formatTime(musicInfo.getDuration()));

        playController = new PlayController(MusicService.player);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                playController.changeMusic(data.get(position).getUrl());
                MainActivity.binder.setPosition(position);
            }
        });
        return view;
    }
}
